package top.suven.base.util.utilize;

import top.suven.base.util.DatePattern;
import top.suven.base.util.TimeConstant;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import static java.util.Calendar.*;

/**
 * 日期工具类
 */
public class DateUtil implements DatePattern {
	private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static String _yyyyMMddHHmm= "yyyy-MM-dd HH:mm";
	private static String _yyyyMMddHHmmss= "yyyy-MM-dd HH:mm:ss";
	private static String yyyyMMddHHmmss="yyyyMMddHHmmssSSS";
	private static final Logger log = LoggerFactory.getLogger(DateUtil.class);

	/**
	 * 检查当前时间和指定时间是否同一周
	 *
	 * @param  year 			年
	 * @param  week 			周
	 * @param  firstDayOfWeek 	周的第一天设置值，{@link Calendar#DAY_OF_WEEK}
	 * @return {@link Boolean}	true-同一周, false-不同周
	 */
	public static boolean isSameWeek(int year, int week, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(firstDayOfWeek);
		return year == cal.get(YEAR) && week == cal.get(WEEK_OF_YEAR);
	}

	/**
	 * 检查当前时间和指定时间是否同一周
	 *
	 * @param  time 			被检查的时间
	 * @param  firstDayOfWeek 	周的第一天设置值，{@link Calendar#DAY_OF_WEEK}
	 * @return {@link Boolean}	true-同一周, false-不同周
	 */
	public static boolean isSameWeek(Date time, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.setFirstDayOfWeek(firstDayOfWeek);
		return isSameWeek(cal.get(YEAR), cal.get(WEEK_OF_YEAR), firstDayOfWeek);
	}

	/**
	 * 获取周的第一天
	 * @param  firstDayOfWeek 	周的第一天设置值，{@link Calendar#DAY_OF_WEEK}
	 * @param  time 			指定时间，为 null 代表当前时间
	 * @return {@link Date}		周的第一天
	 */
	public static Date firstTimeOfWeek(int firstDayOfWeek, Date time) {
		Calendar cal = Calendar.getInstance();
		if (time != null) {
			cal.setTime(time);
		}
		cal.setFirstDayOfWeek(firstDayOfWeek);
		int day = cal.get(DAY_OF_WEEK);

		if (day == firstDayOfWeek) {
			day = 0;
		} else if (day < firstDayOfWeek) {
			day = day + (7 - firstDayOfWeek);
		} else if (day > firstDayOfWeek) {
			day = day - firstDayOfWeek;
		}

		cal.set(HOUR_OF_DAY, 0);
		cal.set(MINUTE, 0);
		cal.set(SECOND, 0);
		cal.set(MILLISECOND, 0);

		cal.add(DATE, -day);
		return cal.getTime();
	}

	/**
	 * 检查指定日期是否今天(使用系统时间)
	 * @param date 被检查的日期
	 * @return
	 */
	public static boolean isToday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.add(DATE, 1);
		cal.set(HOUR_OF_DAY, 0);
		cal.set(MINUTE, 0);
		cal.set(SECOND, 0);
		cal.set(MILLISECOND, 0);
		Date end = cal.getTime(); // 明天的开始
		cal.add(MILLISECOND, -1);
		cal.add(DATE, -1);
		Date start = cal.getTime(); // 昨天的结束
		return date.after(start) && date.before(end);
	}

	/**
	 * 日期转换成字符串格式
	 * @param theDate 待转换的日期
	 * @param datePattern 日期格式
	 * @return 日期字符串
	 */
	public static String date2String(Date theDate, String datePattern) {
		if (theDate == null) {
			return "";
		}

		DateFormat format = new SimpleDateFormat(datePattern);
		try {
			return format.format(theDate);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 字符串转换成日期格式
	 *
	 * @param  dateString 		待转换的日期字符串
	 * @param  datePattern 		日期格式
	 * @return {@link Date}		转换后的日期
	 */
	public static Date string2Date(String dateString, String datePattern) {
		if (dateString == null || dateString.trim().isEmpty()) {
			return null;
		}

		DateFormat format = new SimpleDateFormat(datePattern);
		try {
			return format.parse(dateString);
		} catch (ParseException e) {
			log.error("ParseException in Converting String to date: " + e.getMessage());
		}

		return null;

	}

	/**
	 * 字符串转换成日期格式
	 *
	 * @param  dateString 		待转换的日期字符串
	 * @return {@link Date}		转换后的日期
	 */
	public static Date str2Date(String dateString) {
		if (dateString == null || dateString.trim().isEmpty()) {
			return null;
		}
		DateFormat format = new SimpleDateFormat(DEFAULT_FORMAT);
		try {
			return format.parse(dateString);
		} catch (ParseException e) {
			log.error("ParseException in Converting String to date: " + e.getMessage());
		}

		return null;

	}

	/**
	 * 把秒数转换成把毫秒数
	 *
	 * @param  seconds		秒数的数组
	 * @return {@link Long} 毫秒数
	 */
	public static long toMillisSecond(long...seconds) {
		long millis = 0L;
		if(seconds != null && seconds.length > 0) {
			for (long time : seconds) {
				millis += (time * 1000);
			}
		}
		return millis;
	}

	/**
	 * 把毫秒数转换成把秒数
	 *
	 * @param  millis		毫秒数的数组
	 * @return {@link Long} 毫秒数
	 */
	public static long toSecond(long...millis) {
		long second = 0L;
		if(millis != null && millis.length > 0) {
			for (long time : millis) {
				second += (time / TimeConstant.ONE_SECOND_MILLISECOND);
			}
		}
		return second;
	}

	/**
	 * 修改日期
	 * @param theDate 待修改的日期
	 * @param addYeas 加减的年数
	 * @return 修改后的日期
	 */

	public static Date changeDateOfYear(Date theDate,int addYeas){
		return changeDateOfMonth(theDate,addYeas*12);
	}

	/**
	 * 修改日期
	 * @param theDate 待修改的日期
	 * @param addMonth 加减的月数
	 * @return 修改后的日期
	 */
	public static Date changeDateOfMonth(Date theDate,int addMonth){
		if (theDate == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);

		cal.add(MONTH, addMonth);
		return cal.getTime();
	}

	/**
	 * 把当前时间格式化成yyyy-MM-dd HH:mm:ss
	 *
	 * @return String
	 */
	public static final String date() {
		return date(DEFAULT_FORMAT);
	}

	/**
	 * 把当前时间格式化
	 *
	 * @param format
	 * @return String
	 */
	public static final String date(String format) {
		return date(format, System.currentTimeMillis());
	}

	/**
	 * 把时间戳（秒）格式化成yyyy-MM-dd HH:mm:ss
	 *
	 * @param timestamp
	 * @return String
	 */
	public static final String date(int timestamp) {
		return date(DEFAULT_FORMAT, timestamp);
	}

	/**
	 * 把时间戳（毫秒）格式化成yyyy-MM-dd HH:mm:ss
	 *
	 * @param timestamp
	 * @return String
	 */
	public static final String date(long timestamp) {
		return date(DEFAULT_FORMAT, timestamp);
	}

	/**
	 * 把时间戳格式化
	 *
	 * @param format
	 * @param timestamp
	 *            秒
	 * @return String
	 */
	public static final String date(String format, int timestamp) {
		return date(format, timestamp * 1000l);
	}

	/**
	 * 把时间戳格式化
	 *
	 * @param format
	 * @param timestamp
	 *            毫秒
	 * @return String
	 */
	public static final String date(String format, long timestamp) {
		return new SimpleDateFormat(format).format(timestamp);
	}

	/**
	 * 修改日期
	 * @param theDate 待修改的日期
	 * @param addDays 加减的天数
	 * @param hour 设置的小时
	 * @param minute 设置的分
	 * @param second 设置的秒
	 * @return 修改后的日期
	 */
	public static Date changeDateTime(Date theDate, int addDays, int hour, int minute, int second) {
		if (theDate == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);

		cal.add(DAY_OF_MONTH, addDays);

		if (hour >= 0 && hour <= 24) {
			cal.set(HOUR_OF_DAY, hour);
		}
		if (minute >= 0 && minute <= 60) {
			cal.set(MINUTE, minute);
		}
		if (second >= 0 && second <= 60) {
			cal.set(SECOND, second);
		}

		return cal.getTime();
	}

	public static Date add(Date theDate, int addHours, int addMinutes, int addSecond) {
		if (theDate == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);

		cal.add(HOUR_OF_DAY, addHours);
		cal.add(MINUTE, addMinutes);
		cal.add(SECOND, addSecond);

		return cal.getTime();
	}
	public static Date addMin(Date theDate, int addMinutes) {
		if (theDate == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		cal.add(MINUTE, addMinutes);
		return cal.getTime();
	}

	/**
	 * 取得星期几
	 * @param theDate
	 * @return
	 */
	public static int dayOfWeek(Date theDate) {
		if (theDate == null) {
			return -1;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);

		return cal.get(DAY_OF_WEEK);
	}

	/**
	 * 获得某一时间的0点
	 * @param theDate 需要计算的时间
	 */
	public static Date getDate0AM(Date theDate) {
		if (theDate == null) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(theDate);
		calendar.set(HOUR_OF_DAY, 0);
		calendar.set(MINUTE, 0);
		calendar.set(SECOND, 0);
		calendar.set(MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获得某一时间的下一个0点
	 * @param theDate 需要计算的时间
	 */
	public static Date getNextDay0AM(Date theDate) {
		if (theDate == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(theDate.getTime() + TimeConstant.ONE_DAY_MILLISECOND);
		return new GregorianCalendar(cal.get(YEAR), cal.get(MONTH),	cal.get(DAY_OF_MONTH)).getTime();
	}

	/**
	 * 获得指定日期的23点59分59秒的时间
	 * @param theDate 需要计算的时间
	 */
	public static Date getThisDay2359PM(Date theDate) {
		if (theDate == null) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(theDate);
		calendar.set(HOUR_OF_DAY, 23);
		calendar.set(MINUTE, 59);
		calendar.set(SECOND, 59);
		calendar.set(MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获得当天的指定时间点
	 * @param theDate 需要计算的时间
	 */
	public static Date getSelectTime(Date theDate,int hour,int minute,int second) {
		if (theDate == null) {
			return null;
		}

		if(hour < 0 || minute < 0 || second < 0){
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(theDate);
		calendar.set(HOUR_OF_DAY, hour);
		calendar.set(MINUTE, minute);
		calendar.set(SECOND, second);
		calendar.set(MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 计算2个时间相差的天数,这个方法算的是2个零点时间的绝对时间(天数)
	 * @param startDate 起始时间
	 * @param endDate 结束时间
	 */
	public static int calc2DateTDOADays(Date startDate, Date endDate) {
		if (startDate == null || endDate == null) {
			return 0;
		}
		Date startDate0AM = getDate0AM(startDate);
		Date endDate0AM = getDate0AM(endDate);
		long v1 = startDate0AM.getTime() - endDate0AM.getTime();
		return Math.abs((int) Tools.divideAndRoundUp(v1, TimeConstant.ONE_DAY_MILLISECOND, 0));
	}

	/**
	 * 获得指定时间的下个周一的00:00:00的时间
	 * @param date 指定的时间
	 * @return {@link Date} 周一的00:00:00的时间
	 */
	public static Date getNextMonday(Date date) {
		if (date == null) {
			return null;
		}

		// 本周周一
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtil.getDate0AM(date));
		cal.set(DAY_OF_WEEK, MONDAY);

		Calendar nextMondayCal = Calendar.getInstance();
		nextMondayCal.setTimeInMillis(cal.getTimeInMillis() + TimeConstant.ONE_DAY_MILLISECOND * 7);
		return nextMondayCal.getTime();
	}

	/**
	 * 获得获得改变后的时间
	 *
	 * @param 	addDay			增加的天数(减少天数, 则传负数)
	 * @param   to0AM			是否取0点时间
	 * @return
	 */
	public static Date add(int addDay, boolean to0AM) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(DATE, addDay);
		Date time = calendar.getTime();
		return to0AM ? getDate0AM(time) : time;
	}
	/**
	 * 获得指定时间，改变多少天后的时间；
	 *
	 * @param 	addDay			增加的天数(减少天数, 则传负数)
	 * @param   to0AM			是否取0点时间
	 * @return
	 */
	public static Date add(Date date ,int addDay, boolean to0AM) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(DATE, addDay);
		Date time = calendar.getTime();
		return to0AM ? getDate0AM(time) : time;
	}

	/**
	 * 获得当前时间的秒
	 *
	 * @return {@link Long} 	当前时间的秒
	 */
	public static long getCurrentSecond() {
		return toSecond(System.currentTimeMillis());
	}

	/**
	 * 获得当前时间的毫秒数
	 *
	 * @return {@link Long}
	 */
	public static long getCurrentMillis() {
		return System.currentTimeMillis();
	}

	/**
	 * 获取该日期在当前年中的第几周
	 * @param date
	 * @return int
	 */
	public static int getWeekOfYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setMinimalDaysInFirstWeek(7);
		c.setTime(date);
		return c.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 获取该日期所在年份的最后一周
	 * @param date
	 * @return int
	 */
	public static int getLastWeekOfYear(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
		return getWeekOfYear(c.getTime());
	}
	/**
	 * 寻找最合适的单位来显示时间
	 *
	 * @author ruan 2013-7-21
	 * @param time
	 * @return
	 */
	public static final String showTime(long time) {
		String str = "";
		if (time > 0 && time <= 1000) {
			str = time + " ns";
		} else if (time > 1000 && time <= 1000000) {
			str = new DecimalFormat("0.00").format(time / 1000.0) + " μs";
		} else if (time > 1000000 && time <= 1000000000) {
			str = new DecimalFormat("0.00").format(time / 1000000.0) + " ms";
		} else {
			str = new DecimalFormat("0.00").format(time / 1000000000.0) + " s";
		}
		return str;
	}

	/**
	 * 获取当月最后一天是多少号
	 * @author ruan
	 */
	public static int getLastDayOfMonth() {
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTimeInMillis(System.currentTimeMillis());
		return cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取下个月
	 * @author ruan
	 * @return
	 */
	public final static String getNextMonth() {
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		c.add(Calendar.MONTH, 1);
		return date2String(c.getTime(),DatePattern.PATTERN_YYYYMM);
	}

	/**
	 * 获取上个月
	 * @author ruan
	 * @return
	 */
	public final static String getLastMonth() {
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		c.add(Calendar.MONTH, -1);
		return date2String(c.getTime(),DatePattern.PATTERN_YYYYMM);
	}

	/**
	 * 判断是否是本月最后一天
	 *
	 * @return
	 */
	public static boolean isLastDayOfMonth() {
		Calendar c=Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		Calendar c1=(Calendar)c.clone();
		c.add(Calendar.DAY_OF_MONTH,1);
		return c.get(Calendar.MONTH)!=c1.get(Calendar.MONTH);
	}

	/**
	 * 判断是否是本月第一天
	 *
	 * @return
	 */
	public static boolean isFirstDayOfMonth() {
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		Calendar c1 = (Calendar)c.clone();
		c.add(Calendar.DAY_OF_MONTH, -1);
		return c.get(Calendar.MONTH)!=c1.get(Calendar.MONTH);
	}

	public static int getCurrDate(){
		Calendar c=Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 日期格式 01
	 * @return
	 */
	public static String getCurrentDate(){
		Calendar c=Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		return date2String(date,DatePattern.PATTERN_DD);
	}
	/**
	 * 获取当前年份
	 * @return
	 */
	public static int getCurrYear(){
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 获取当前月份
	 * @return
	 */
	public static int getCurrMonth(){
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		return c.get(Calendar.MONTH)+1;
	}

	/**
	 * 字符串转时间戳
	 *
	 * @param str
	 *            字符串
	 * @param format
	 *            时间格式
	 * @param isMicrotime
	 *            是否输出毫秒
	 */
	public static final long str2time(String str, String format, boolean isMicrotime) {
		if (str == null || "".equals(str)) {
			return -1;
		}
		try {
			if (isMicrotime) {
				return new SimpleDateFormat(format).parse(str).getTime();
			}
			return new SimpleDateFormat(format).parse(str).getTime() / 1000;
		} catch (Exception e) {
			return -1;
		}
	}

	/**
	 * 字符串转时间戳
	 *
	 * @param str
	 *            字符串
	 * @param format
	 *            时间格式
	 */
	public static final int str2time(String str) {
		return (int) str2time(str, DEFAULT_FORMAT, false);
	}

	/**
	 * 字符串转时间戳
	 *
	 * @param str
	 *            字符串
	 * @param isMicrotime
	 *            是否输出毫秒
	 */
	public static final long str2time(String str, boolean isMicrotime) {
		return str2time(str, DEFAULT_FORMAT, isMicrotime);
	}

	/**
	 * 字符串转时间戳
	 *
	 * @param str
	 *            字符串
	 * @param format
	 *            时间格式
	 */
	public static final int str2time(String str, String format) {
		return (int) str2time(str, format, false);
	}

	/**
	 * 得到本周的第一天
	 * @return
	 */
	public static String getCurrWeekBegin() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, calendar
				.getActualMinimum(Calendar.DAY_OF_WEEK));

		return date2String(calendar.getTime(),DatePattern.PATTERN_YYYY_MM_DD);
	}

	/**
	 * 得到本周的最后一天
	 *
	 * @return
	 */
	public static String getCurrWeekEnd() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, calendar
				.getActualMaximum(Calendar.DAY_OF_WEEK)+7);
		return date2String(calendar.getTime(),DatePattern.PATTERN_YYYY_MM_DD);
	}

	/**
	 * 获取当前小时
	 * @return
	 */
	public static int getCurrHour(){
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取三月前的时间
	 *
	 * @return
	 */
	public static long getThreeMonthAgo() {
		Calendar c=Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		c.add(Calendar.MONTH,-3);
		return c.getTimeInMillis();
	}

	/**
	 * 将时间转化成 "yyyy-MM-dd"格式的字符串
	 * @param date
	 * @return
	 */
	public static String YMDToString(Date date){
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		return dateformat.format(date);
	}

	/**
	 * 将时间转换成‘yyyy-MM-dd HH24:mm ’格式的字符串
	 * @param date
	 * @return
	 */
	public static String YMDHMToString(Date date){
		if (date == null) {
			return "";
		}
		DateFormat dateformat = new SimpleDateFormat(DEFAULT_FORMAT);
		return dateformat.format(date);
	}

	public static Date getTomorrowStartDate(Date theDate){//获得指定日期的明天开始时间0点0分0秒的时间
		if (theDate == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		cal.add(DATE, 1);
		cal.set(HOUR_OF_DAY, 0);
		cal.set(MINUTE, 0);
		cal.set(SECOND, 0);
		cal.set(MILLISECOND, 0);
		Date tomorrowStartDate = cal.getTime(); //明天的开始时间
		return tomorrowStartDate;
	}

	/**
	 * 获取日期, 以参考日期date为标准进行日期增减。
	 * @param offset 日期偏移量，昨天是－1
	 * @return  yyyy-MM-dd
	 */
	public static String yyyy_MM_dd(final int offset) {
		final Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, offset);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(cal.getTime());
	}

	/**
	 * 获取当前日期的无分隔符全数字格式
	 * @return yyyyMMddHHmmss
	 */
	public static String getNowToNumFormat(){
		return date2String(new Date(),"yyyyMMddHHmmss");
	}

	public static void main(String[] args) {
//		System.out.println(DateUtil.date(1439479911));
//		System.out.println(DateUtil.date(1441092665));
//		System.out.println(new Date().getTime()/1000+20000);
//		System.out.println(DateUtil.date2String(theDate, datePattern));
//		System.out.println(between());

		System.out.println(date2String(getNextMounthFirstDay(),DEFAULT_FORMAT));

	}

	public static boolean between(){
		SimpleDateFormat df=new SimpleDateFormat(DatePattern.PATTERN_NORMAL);
		try {
			Date dateAfter=df.parse("2015-09-05 23:59:59");
			Date dateBefor=df.parse("2015-09-03 00:00:00");
			Date currDate=new Date();
			if(currDate.before(dateAfter) && currDate.after(dateBefor)){
				return true;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		return false;

	}

	/**
	 * 把时间戳转成 HH:mm 的字符串
	 * @param time
	 */
	public static String toHHmm(long time) {
		StringBuffer t = new StringBuffer();
		long h = time / 1000 / 60 / 60;
		long m = time / 1000 / 60;
		if(m >= 60){
			m = m % 60;
		}
		if(h < 10){
			t.append("0");
		}
		t.append(h + ":");
		if(m < 10){
			t.append("0");
		}
		t.append(m);
		return t.toString();
	}

	/**
	 * @Title:
	 * @Description: 根据时间戳转成规定格式
	 * @param
	 * @return
	 * <p>
	 *  --------------------------------------------------------
	 *  modifyer    modifyTime                 comment
	 *
	 *  --------------------------------------------------------
	 * </p>
	 * @author liulu
	 * @date 2018/5/15 19:20
	 */

	public static String getDate(long millis) {
		if (millis < 0) {
			return null;
		}
		return DateFormatUtils.format(millis, _yyyyMMddHHmm);
	}


	/**
	 * 得到指定月的天数
	 * */
	public static int getMonthLastDay(int year, int month){
		Calendar a = Calendar.getInstance();
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);//把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

	/**
	 * 时间格式转换
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String date){
		try {
			return new SimpleDateFormat(_yyyyMMddHHmmss).parse(date);
		} catch (ParseException e) {
			log.error("ParseException in Converting String to date: " + e.getMessage(),e);
		}
		return null;
	}

	/**
	 * 获取当月第一天时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getMounthFirstDay() {
		SimpleDateFormat dateFormat =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DAY_OF_MONTH, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String dateStr = sdf.format(cal.getTime());
		try {
			return dateFormat.parse(dateStr);
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 获取下个月第一天时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getNextMounthFirstDay() {
		SimpleDateFormat dateFormat =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.MONTH,1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String dateStr = sdf.format(cal.getTime());
		try {
			return dateFormat.parse(dateStr);
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 获取当月最后一天时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getMounthLatDay() {
		SimpleDateFormat dateFormat =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.roll(Calendar.DAY_OF_MONTH, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		String dateStr = sdf.format(cal.getTime());
		Date parse = null;
		try {
			parse = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return parse;
	}

	/**
	 * 获取两个时间的相差秒
	 * @param beforDate
	 * @param afterDate
	 * @return
	 */
	public static int getTimeSub(Date beforDate,Date afterDate){
		if(null == beforDate || null == afterDate){
			return 0;
		}

		long beforDateTime = beforDate.getTime();
		long afterDateTime = afterDate.getTime();

		if(beforDateTime > afterDateTime){
			return 0;
		}

		long sub = afterDateTime - beforDateTime;
		if(sub >= 1000){//不足一秒，直接设置为0
			return new Double(sub / 1000).intValue();
		}
		return 0;
	}

}
